<script setup lang="ts" generic="ContentType extends string | number">
import { ref } from "vue";

// 定义 Props 类型
const props = defineProps({
  modelValue: {
    type: Boolean,
    required: true,
  },
  title: {
    type: String,
    default: "提示",
  },
  closeOnClickMask: {
    type: Boolean,
    default: true,
  },
});

// 定义 Emits 类型
const emit = defineEmits<{
  (e: "update:modelValue", value: boolean): void;
  (e: "confirm", content: ContentType | null): void;
  (e: "cancel"): void;
}>();

// 组件内部状态
const content = ref<ContentType | null>(null);
const isVisible = ref(false);

// 暴露给父组件的方法
const open = (newContent: ContentType) => {
  content.value = newContent;
  isVisible.value = true;
  emit("update:modelValue", true);
};

const close = () => {
  isVisible.value = false;
  emit("update:modelValue", false);
  emit("cancel");
};

const confirm = () => {
  emit("confirm", content.value);
  close();
};

// 点击遮罩层处理
const handleMaskClick = () => {
  if (props.closeOnClickMask) {
    close();
  }
};

// 暴露方法给父组件
defineExpose({
  open,
  close,
});
</script>

<template>
  <Teleport to="body">
    <Transition name="modal-fade">
      <div v-if="modelValue" class="modal-mask" @click.self="handleMaskClick">
        <div class="modal-container">
          <div class="modal-header">
            <h3>{{ title }}</h3>
            <button class="modal-close" @click="close">×</button>
          </div>

          <div class="modal-body">
            <slot :content="content">
              <p>默认内容: {{ content }}</p>
            </slot>
          </div>

          <div class="modal-footer">
            <slot name="footer">
              <button @click="close">取消</button>
              <button @click="confirm">确定</button>
            </slot>
          </div>
        </div>
      </div>
    </Transition>
  </Teleport>
</template>

<style scoped>
.modal-mask {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 9999;
}

.modal-container {
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.33);
  width: 80%;
  max-width: 500px;
  padding: 20px;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  border-bottom: 1px solid #eee;
  padding-bottom: 10px;
}

.modal-header h3 {
  margin: 0;
}

.modal-close {
  background: none;
  border: none;
  font-size: 24px;
  cursor: pointer;
}

.modal-body {
  margin-bottom: 20px;
}

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  border-top: 1px solid #eee;
  padding-top: 10px;
}

.modal-footer button {
  padding: 8px 16px;
  border-radius: 4px;
  cursor: pointer;
}

/* 过渡动画 */
.modal-fade-enter-active,
.modal-fade-leave-active {
  transition: opacity 0.3s ease;
}

.modal-fade-enter-from,
.modal-fade-leave-to {
  opacity: 0;
}

.modal-fade-enter-active .modal-container,
.modal-fade-leave-active .modal-container {
  transition: transform 0.3s ease;
}

.modal-fade-enter-from .modal-container,
.modal-fade-leave-to .modal-container {
  transform: scale(0.95);
}
</style>
